package com.bond.cloud.producer;

import com.bond.cloud.feign.GoodsFeignClient;
import com.bond.cloud.feign.OrderFeignClient;
import com.bond.cloud.po.TestData;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeataTransactionServiceImpl {

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @GlobalTransactional(rollbackFor = Exception.class)
    public TestData seataTransaction() {

        System.out.println(("当前的XID：" + RootContext.getXID()));
        TestData data = new TestData();
        data.setId(1000L);

        goodsFeignClient.save(data);

        orderFeignClient.save(data);

        // 测试事务回滚,此处发生异常
        int a = 1 / 0;

        return null;
    }
}
